<?php

abstract class Entity extends BaseEntity
{	
	protected $active = 1;
	
	/**
	 * getter id
	 *
	 * @return id
	 */
	public function getId()
	{
		return $this->id;
	}
	
	/**
	 * Setter id
	 *
	 * @param int id
	 */
	public function setId($id)
	{
		$this->id = $id;
	}
	
	/**
	 * getter active
	 *
	 * @return int
	 */
	public function getActive()
	{
		return $this->active;
	}
	
	/**
	 * Setter active
	 *
	 * @param int active
	 */
	public function setActive($active)
	{
		$this->active = $active;
	}
	
	/**
	 * getter created
	 *
	 * @return created
	 */
	public function getCreated()
	{
		if($this->created == "")
			return $this->created = date("Y-m-d H:i:s");
		else
			return $this->created;
	}
	
	/**
	 * Setter created
	 *
	 * @param Date created
	 */
	public function setCreated($created)
	{
		$this->created = $created;
	}
	
	/**
	 * getter updated
	 *
	 * @return Date
	 */
	public function getUpdated()
	{
		if(Update::$doingUpdate)
			return date("Y-m-d H:i:s");
			
		if($this->updated == "")
			return $this->updated = date("Y-m-d H:i:s");
		else
			return $this->updated;
	}
			
	/**
	 * Setter updated
	 *
	 * @param  Date updated
	 */
	public function setUpdated($updated)
	{
		$this->updated = $updated;
	}
	
	protected function __meta()
	{
		$i = new TInt('id');
		$i->isPrimary = true;
		$i->isAutoIncrement = true;
		$i->default = null;
		Map::setField($this,$i);	
		
		Map::setField($this,new TInt('active',1,1));
		
		Map::setField($this,new TDate('created'));
		
		Map::setField($this,new TDate('updated'));
		
	}	
	
	public function __normalise()
	{
		$array = array();
		$fields = Map::getFields(get_class($this));

		foreach($fields as $key => $field)
		{
			if($field instanceof EagerJoin)
			{
				if(isset($this->{$field->field}))
				{
					$array[$key] = $this->{$field->field}->__toString();
				} else {
					$array[$key] = $this->{$field->fieldPrefix.$field->field};
				}
			} else if(!($field instanceof Join))
			{
				$array[$key] = $this->{$field->field};
			}
		}
		
		return $array;
	}

	public function __toString()
	{
		return $this->__normalise();
	}
}


?>